package com.ojoin.ojoin.eb_spider.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.DataCheckService;
import com.ojoin.ojoin.eb_spider.service.StatTotalService;
import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlStatetaskRunresultvo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
@Service
public class DataCheckServiceImpl implements DataCheckService {

	@Autowired
	private StatTotalService statTotalService;

	@Override
	public String getDataCheck(TbMerchantInfo tm, int joinid) throws Exception {
		int count = 40;
		String stime = TimeUtils.getDateBefore(count);
		
		Map<Integer,TbSlStatetaskRunresultvo> runresult = WebServiceUtils.queryByPlatformAndMerchantUserIDAndDate(null, joinid+"", TimeUtils.getCurDate());
		
		List<TbSlPlatformUser> users = WebServiceUtils
				.getPlatformUserList(joinid);
		StringBuffer html = new StringBuffer();
		if (!CommonUtils.isEmpty(users)) {
			
			html.append("<html><body><table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">");
			StringBuffer shopName = new StringBuffer("<tr><td>单位名称</td>");
			
			StringBuffer time = new StringBuffer("<tr><td>统计时间</td>");
			
			StringBuffer plateFormName = new StringBuffer("<tr><td>平台名称</td>");

			StringBuffer username = new StringBuffer("<tr><td>登陆名</td>");
			StringBuffer bank = new StringBuffer("<tr><td>开户银行</td>");
			StringBuffer banknum = new StringBuffer("<tr><td>账号：</td>");
			StringBuffer pliabilities = new StringBuffer("<tr><td>平台贷款</td>");
			StringBuffer waitpay = new StringBuffer(
					"<tr><td>待结算已开票总金额</td>");
			StringBuffer checkpay = new StringBuffer(
					"<tr><td>待结算未开票总金额</td>");

			StringBuffer inventory = new StringBuffer("<tr><td>库存</td>");
			
			StringBuffer inventoryOut = new StringBuffer("<tr><td>当日退货</td>");
			
			StringBuffer totalAll = new StringBuffer("<tr><td>应收总额</td>");
			StringBuffer liabilities = new StringBuffer("<tr><td>负债总额</td>");
			StringBuffer pay = new StringBuffer("<tr><td>已付款金额("+count+"天)</td>");
			
			StringBuffer repayment = new StringBuffer("<tr><td>已还款总金额("+count+"天)</td>");
			StringBuffer payAll = new StringBuffer("<tr><td>已付款总金额("+count+"天)</td>");
			
			Iterator<TbSlPlatformUser> it = users.iterator();
			
			long liabilitie = WebServiceUtils.getLiabilitiesByJoinId(joinid);
			
			long repaymentMoney = WebServiceUtils.getLoanRefundMoney(joinid, stime, TimeUtils.getCurDate());

			long totall = 0 ;
			long payall = 0 ;
			while (it.hasNext()) {
				TbSlPlatformUser user = it.next();

				TbCatchdataStrategy ca = WebServiceUtils.getCatchdataStrategyByUserId(user.getId());
				
				TbSlLoanStatTotal total = statTotalService.stat(user);
				
				long pliabilitie = WebServiceUtils
						.getSupplyPaymentMoneyByplateformId(user.getId(),ca, null,
								null, TbSlSupplyPayment.FINANCING);

				long paymoney = WebServiceUtils.getSupplyPaymentMoneyByplateformId(user.getId(),ca, stime, TimeUtils.getCurDate(), TbSlSupplyPayment.COMPLETEPAY);
				
				plateFormName.append("<td>"+user.getPlatformName()+"</td>");
				
				username.append("<td>"+user.getUsername()+"</td>");
				
				if(!CommonUtils.isEmpty(runresult) && !CommonUtils.isEmpty(runresult.get(user.getId()))){
					String memo = runresult.get(user.getId()).getMemo();
					Map map = JSONUtils.parseObject(memo, Map.class);
					if(!CommonUtils.isEmpty(map)){
						bank.append("<td>"+map.get("bank")+"</td>");
						banknum.append("<td>"+map.get("bankNum")+"</td>");
					}else {
						bank.append("<td></td>");
						banknum.append("<td></td>");
					}
					
				}else {
					bank.append("<td></td>");
					banknum.append("<td></td>");
				}
				
				
				pliabilities.append("<td>"+pliabilitie/100+"</td>");
				waitpay.append("<td>"+total.getTotalUnpaySettleorder()/100+"</td>");
				checkpay.append("<td>"+total.getTotalUnpaySettleorderCheck()/100+"</td>");
				inventory.append("<td>"+total.getTotalUnsettleInstoreorder()/100+"</td>");
				inventoryOut.append("<td >"+total.getTotalUnenterinRefundorder()/100+"</td>");
				pay.append("<td>"+paymoney/100+"</td>");
				totall +=total.getTotalAll();
				payall += paymoney;
				
			}
			
			shopName.append("<td colspan="+users.size()+">"+tm.getMchName()+"</td>");
			time.append("<td colspan="+users.size()+">"+TimeUtils.getCurDate()+"</td>");
			totalAll.append("<td colspan="+users.size()+">"+totall/100+"</td>");
			liabilities.append("<td colspan="+users.size()+">"+liabilitie/100+"</td>");
			
			repayment.append("<td colspan="+users.size()+">"+repaymentMoney/100+"</td>");
			payAll.append("<td colspan="+users.size()+">"+payall/100+"</td>");
			
			
			
			
			shopName.append("</tr>");
			time.append("</tr>");
			plateFormName.append("</tr>");
			
			username.append("</tr>");
			bank.append("</tr>");
			banknum.append("</tr>");
			
			pliabilities.append("</tr>");
			
			waitpay.append("</tr>");
			checkpay.append("</tr>");
			inventory.append("</tr>");
			inventoryOut.append("</tr>");
			pay.append("</tr>");
			totalAll.append("</tr>");
			liabilities.append("</tr>");
			repayment.append("</tr>");
			
			
			return html.append(shopName).append(time).append(plateFormName).append(username).append(bank).append(banknum)
					.append(pliabilities).append(waitpay).append(checkpay).append(inventory).append(inventoryOut).append(pay)
					.append(totalAll).append(liabilities).append(payAll).append(repayment)
					.append("</table></body></html>").toString();

		}

		return null;

	}

	@Override
	public String getDataCheck(int joinid) throws Exception {
		
		TbMerchantInfo tm = WebServiceUtils.getTbMerchantUserByJoinId(joinid);
		
		return getDataCheck(tm,joinid);
	}

}
